<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/css/userRole.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/style_table.css}"/>
</head>
<style type="text/css">
    .form-horizontal {
        margin: 0;
        padding: 20px 50px
    }

    .form-group label {
        margin-top: 7px
    }

    .close_btn{
        height: 30px;
        width: 55px;
        position: absolute;
        right: 80px;
    }

</style>
<body>

<form id="fm" class="form-horizontal" novalidate>
    <div class="form-group">
        <div class="col-xs-9">
            <input type="hidden" name="id" id="id" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-9">
            <input type="hidden" name="teacherId" id="teacherId" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">姓名:</label>
        <div class="col-xs-9">
            <input type="text" name="teacherName" id="teacherName" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">性别:</label>
        <div class="col-xs-9">
            <input type="text" name="teacherSex" id="teacherSex" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">电话:</label>
        <div class="col-xs-9">
            <input type="text" name="tel" id="tel" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">家庭住址:</label>
        <div class="col-xs-9">
            <input type="text" name="address" id="address" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">身份证号:</label>
        <div class="col-xs-9">
            <input type="text" name="cardNo" id="cardNo" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">所属教师组:</label>
        <div class="col-xs-9">
            <input type="text" name="groupBelongs" id="groupBelongs" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">教授课程:</label>
        <div class="col-xs-9">
            <input type="text" name="curriculum" id="curriculum" class="form-control ">
        </div>
    </div>
    </div>
    <button id="save" class="btn btn-primary" type="button" onclick="updateTeacher();">保存</button>
    <button id="hide" class="btn btn-warning close_btn" type="button" onclick="parent.closeWindow();">关闭</button>
</form>

</body>
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/plugin/easyui/jquery.easyui.min.js}"></script>
<script type="text/javascript" th:src="@{/plugin/layer/layer.js}"></script>
<script th:inline="javascript">
    $(function () {
        var tId = sessionStorage.getItem("tId");
        $("#id").val(tId);
        console.log("tId=" + tId);

        //填充表单
        $.ajax({
            url: "/teacher/getTeacherById",
            type: "GET",
            data: {
                "id" : tId
            },
            dataType: "json",
            success: function (result) {
                if(result.msg === 200){
                    $("#teacherName").val(result.data.teacherName);
                    $("#teacherSex").val(result.data.teacherSex);
                    $("#tId").val(result.data.tId);
                    $("#tel").val(result.data.tel);
                    $("#teacherId").val(result.data.teacherId);
                    $("#address").val(result.data.address);
                    $("#cardNo").val(result.data.cardNo);
                    $("#groupBelongs").val(result.data.groupBelongs);
                    $("#curriculum").val(result.data.curriculum);

                }
            }
        });

    });

    /**
     * 空值校验
     * @returns {boolean}
     */
    function checkForm() {
        var teacherName = $("#teacherName").val();
        var teacherSex = $("#teacherSex").val();
        var tel = $("#tel").val();
        var address = $("#address").val();
        var cardNo = $("#cardNo").val();
        var groupBelongs = $("#groupBelongs").val();
        var curriculum = $("#curriculum").val();
        if(parent.isEmpty(teacherName)){
            layer.msg("请输入教师姓名");
            return false
        }else if(parent.isEmpty(teacherSex)){
            layer.msg("请输入教师性别");
            return false
        }else if(parent.isEmpty(tel)){
            layer.msg("请输入电话号码");
            return false
        }else if(parent.isEmpty(address)){
            layer.msg("请输入家庭住址");
            return false
        }else if(parent.isEmpty(cardNo)){
            layer.msg("请输入身份证号");
            return false
        }else if(parent.isEmpty(groupBelongs)){
            layer.msg("请输入所属教师组");
            return false
        }else if(parent.isEmpty(curriculum)){
            layer.msg("请输入教授课程");
            return false
        }
        return true;
    }

    //提交表单
    function updateTeacher() {
        if(checkForm()){
            $.ajax({
                type: "POST",
                dataType : "json",
                url: "/teacher/updateTeacher",
                data: $("#fm").serialize(),
                success: function (result) {
                    if(result.msg === 200){
                        parent.searchTeacher(15,1);
                        parent.closeWindow("修改成功");
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
        }

    }

</script>
</html>